import sys
NeededPaths=['/opt/coopip/djangoproject/include', '/opt/coopip/djangoproject', '/opt/cis', '/opt/oms/', '/opt/amr', '/opt/gis','/opt/gos', '/opt']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
import httplib
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, datetime
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib.gis import admin
import _mysql as mysql
from django.db import transaction
import django, urllib
import COOPIP_Form
import CIS, AMR, GIS, GOS  #Comment this out before doing a syncdb



class Department(models.Model):
    """List of TCEC Departments"""
    using='default'
    Name=models.TextField(db_column='name', unique=True)
    GroupName=models.TextField(db_column='groupname')  #Employees will be members of departments based on GroupNames
    EditorGroupName=models.TextField(db_column='editorgroupname')
    BackgroundImageFileName=models.TextField(db_column='backgroundimagefilename', blank=True, null=True)
    objects=models.GeoManager()
    class Meta:
        db_table='department'
        app_label='djangoproject'
    def __unicode__(self):
        return self.Name

class Message(models.Model):
    """Messages that may be displayed on the Department Sites or on the main page.  """
    using='default'
    Title=models.TextField(db_column='title')
    MessageText=models.TextField(db_column='messagetext')
    FrontPageIndicator=models.NullBooleanField(db_column='frontpageindicator', null=True, blank=True)
    DepartmentPageIndicator=models.NullBooleanField(db_column='departmentpageindicator', null=True, blank=True)
    Department=models.ForeignKey(Department, db_column='department')
    Priority=models.FloatField(db_column='priority', null=True, blank=True)
    CreateUsername=models.TextField(db_column='createusername')
    CreateDateTime=models.DateTimeField(db_column='createdatetime', null=True, blank=True)
    LastEditUsername=models.TextField(db_column='lasteditusername', null=True, blank=True)
    LastEditDateTime=models.DateTimeField(db_column='lasteditdatetime', null=True, blank=True)
    ExpireDateTime=models.DateTimeField(db_column='expiredatetime', null=True, blank=True)
    ArchiveIndicator=models.NullBooleanField(db_column='archiveindicator', null=True, blank=True)
    objects=models.GeoManager()
    class Meta:
        db_table='message'
        app_label='djangoproject'
    def GetAllEditableMessages(self, User):
        Groups=User.groups.all()
        Departments=[]
        for group in Groups:
            for department in Department.objects.filter(EditorGroupName=group):
                Departments.append(department)
        Messages=Message.objects.all()
        AllDepartments=Department.objects.all()
        for department in AllDepartments:
            if(not department in Departments):
                Messages=Messages.exclude(Department=department)
        return Messages
    def __unicode__(self):
        if(self.ArchiveIndicator):
            String='*'
        else:
            String=''
        return str(String + str(self.id)+ ":  "  + str(self.Title))



class Event(models.Model):
    using='default'
    DoNotShowBeforeDateTime=models.DateTimeField(db_column='donotshowbeforedatetime', null=True, blank=True)
    StartDateTime=models.DateTimeField(db_column='eventstartdatetime', null=True, blank=True)
    EndDateTime=models.DateTimeField(db_column='eventenddatetime', null=True, blank=True)
    ArchiveOverrideDateTime=models.DateTimeField(db_column='archiveoverridedatetime', null=True, blank=True)
    Name=models.TextField(db_column='eventname')
    Resource=models.TextField(db_column='resource', null=True, blank=True)
    Department=models.ForeignKey('Department', db_column='department')
    FrontPageIndicator=models.NullBooleanField(db_column='frontpageindicator', null=True, blank=True)
    ArchiveIndicator=models.NullBooleanField(db_column='archiveindicator', null=True, blank=True)
    StaffOnlyIndicator=models.NullBooleanField(db_column='staffonlyindicator', null=True, blank=True)
    objects=models.GeoManager()
    class Meta:
        db_table='event'
        app_label='djangoproject'
    def GetAllEditableEvents(self, User):
        Groups=User.groups.all()
        Departments=[]
        for group in Groups:
            for department in Department.objects.filter(EditorGroupName=group):
                Departments.append(department)
        Events=Event.objects.all()
        AllDepartments=Department.objects.all()
        for department in AllDepartments:
            if(not department in Departments):
                Events=Events.exclude(Department=department)
        return Events
    def __unicode__(self):
        if(self.ArchiveIndicator):
            String='*'
        else:
            String=''
        return str(String + str(self.id) + ':  ' + str(self.Name))


class MessageFile(models.Model):
    """MessageFile are files which may be attached to messages, the permissions on files will be based on the message permissions"""
    using='default'
    FileName=models.TextField(db_column='filename', null=True, blank=True)
    Message=models.ForeignKey('Message', db_column='message', null=True, blank=True)
    File=models.FileField(max_length=250, upload_to='message', null=True, blank=True)
    DateTime=models.DateTimeField(db_column='datetime', null=True, blank=True)
    objects=models.GeoManager()
    class Meta:
        db_table=u'messagefile'
        app_label='djangoproject'
    def __unicode__(self):
        return str(self.Message) + ' ' + str(self.FileName)


class DispatchBoard(models.Model):
    """DispatchBoard is used by dispatch for displaying various information about who is on call, crews, and who is out of the office."""
    using='default'
    CrewList=models.TextField(db_column='crewlist', null=True, blank=True)
    EngineerList=models.TextField(db_column='engineerlist', null=True, blank=True)
    ServiceWorkerList=models.TextField(db_column='serviceworkerlist', null=True, blank=True)
    Message=models.TextField(db_column='message', null=True, blank=True)
    OutOfOffice=models.TextField(db_column='outofoffice', null=True, blank=True)
    ServiceWorkerHours=models.TextField(db_column='serviceworkerhours', null=True, blank=True)
    OnCallList=models.TextField(db_column='oncalllist', null=True, blank=True)
    CreateUsername=models.TextField(db_column='createusername')
    CreateDateTime=models.DateTimeField(db_column='createdatetime', null=True, blank=True)
    class Meta:
        db_table=u'dispatchboard'
        app_label='djangoproject'
    def __unicode__(self):
        return 'Single Row Table:  ' + str(self.id)


class DispatchCrewLog(models.Model):
    """DispatchCrewLog is used throughout the day by dispatch for tracking when crews are en route, arrive at, and leave various work sites."""
    using='default'
    CrewNumber=models.TextField(db_column='crewnumber', null=True, blank=True)
    TruckNumber=models.TextField(db_column='trucknumber', null=True, blank=True)
    TruckNumberLastModifyDateTime=models.DateTimeField(db_column='trucknumberlastmodifydatetime', null=True, blank=True)
    Employees=models.TextField(db_column='employees', null=True, blank=True)
    EnRouteDateTime=models.DateTimeField(db_column='enroutedatetime', null=True, blank=True)
    ArriveAtLocationDateTime=models.DateTimeField(db_column='arriveatlocationdatetime', null=True, blank=True)
    DepartLocationDateTime=models.DateTimeField(db_column='departlocationdatetime', null=True, blank=True)
    LocationDescription=models.TextField(db_column='locationdescription', null=True, blank=True)
    PoleNumber=models.IntegerField(db_column='polenumber', null=True, blank=True)
    Address=models.TextField(db_column='address', null=True, blank=True)
    BackAtOfficeDateTime=models.DateTimeField(db_column='backatofficedatetime', null=True, blank=True)
    Comments=models.TextField(db_column='comments', null=True, blank=True)
    CreateDateTime=models.DateTimeField(db_column='createdatetime', null=True, blank=True)
    LastModifyDateTime=models.DateTimeField(db_column='lastmodifydatetime', null=True, blank=True)
    DeletedIndicator=models.NullBooleanField(db_column='deletedindicator', null=True, blank=True)
    class Meta:
        db_table=u'dispatchcrewlog'
        app_label='djangoproject'
    def __unicode__(self):
        return str(self.CreateDateTime) + ' ' + str(self.CrewNumber)


class DispatchMapLabel(models.Model):
    """DispatchMapLabel is used on the static system display.  Paths are used to draw bitmaps at specified points."""
    Name=models.TextField(db_column='name', null=True, blank=True)
    Point=models.PointField(db_column='the_geom', srid=2273, null=True, blank=True)
    Description=models.TextField(db_column='description', null=True, blank=True)
    Symbology=models.TextField(db_column='symbology', null=True, blank=True)
    SymbologyColor=models.TextField(db_column='symbologycolor', null=True, blank=True)
    class Meta:
        db_table=u'dispatchmaplabel'
        app_label='djangoproject'


        
